<template>
  <div
    :style="{padding: padding || '24px', height: (detail && cardH)? cardH +' !important': (detail && !cardH)? 'calc(100vh - 96px) !important': (!detail && cardH)? cardH : 'auto', borderRadius: radius || '16px'}" 
    :class="['b-card', {'b-card-detail': detail}]"
  >
    <div
      v-if="(refresh || false) && scrollD < 32"
      class="fontTheme refresh-btn"
      @click="handleRefresh"
    >
      <i class="iconfont icon-Save"></i>刷新
    </div>
    <div v-if="title || btnList.length > 0" class="card-head">
      <div class="row row_flex title-content" v-if="title || btnList.length > 0">
        <div class="row_flex">
          <div class="title" style="padding-bottom:24px;" v-if="title">{{ title }}</div>
          <div v-if="subtitle" class="subtitle fontTheme">{{ subtitle }}</div>
        </div>
        <div v-if="btnList.length > 0" class="btn">
          <div v-for="(item, index) in btnList" :key="index">
            <button-tree :btnCode="item.btnCode">
              <div v-if="item.isLoading" slot="visible_true" style="padding-bottom:16px;">
                <b-button
                  v-if="!item.uploadUrl"
                  style="margin-left: 10px;"
                  :isHollow="item.isHollow"
                  :text="item.name"
                  @click="btnFun(item)"
                ></b-button>
              </div>
              <div v-else slot="visible_true" style="padding-bottom:16px;">
                <b-button
                  v-if="!item.uploadUrl"
                  :isHollow="item.isHollow"
                  style="margin-left: 10px;"
                  @click="btnFun(item)"
                  :text="item.name"
                ></b-button>
              </div>
            </button-tree>
          </div>
        </div>
      </div>
    </div>
    <div class="slot-content" :style="{height: (detail&&noBtn)? 'calc(100vh - 144px)': (detail&&scrollHeight)? scrollHeight : (detail&&!scrollHeight)?'calc(100vh - 118px)':'auto', paddingBottom: (detail && !noBtn)?'32px':''}">
      <template v-if="isOpen">
        <k-transition>
          <div v-show="show">
            <slot />
          </div>
        </k-transition>
      </template>
      <template v-else>
        <slot />
      </template>
    </div>
    <div class="btn-wrapper" v-if="!noBtn">
      <slot name="btn-wrapper" />
    </div>
  </div>
</template>
  
  <script>
export default {
  name: "BCard",
  props: {
    // 标题
    title: {
      type: String,
      default: ""
    },
    // 副标题
    subtitle: {
      type: String,
      default: ""
    },
    // 是否删除原有card的margin和padding和box-shadow  利用嵌套使用
    isDelOriginalCard: {
      type: Boolean,
      default: function() {
        return false;
      }
    },
    // card title一栏中右边的按钮集合
    btnList: {
      type: Array,
      default: function() {
        return [];
      }
    },
    // 是否可以展开收起card
    isOpen: {
      type: Boolean,
      default: function() {
        return false;
      }
    },
    // 是否没有按钮
    noBtn: {
      type: Boolean,
      default: function() {
        return false;
      }
    },
    /// 刷新
    refresh: {
      type: Boolean,
      default: function() {
        return false;
      }
    },
    // 详情 - 高度只展示一屏
    detail: {
      type: Boolean,
      default: false
    },
    // // 是否展示一屏  局部滚动
    // isDetailCard: {
    //   type: Boolean,
    //   default: false
    // },
    // 动态padding值
    padding: {
      type: String,
      default: "24px"
    },
    // 滚动区域高度
    scrollHeight: {
      type: String,
      default: "calc(100vh - 220px)"
    },
    // card 高度
    cardH: [String],
    // 圆角
    radius: [String],
    // onlyListCard: [Boolean]
  },
  data() {
    return {
      show: true,
      scrollD: ""
    };
  },
  mounted() {
    if (this.refresh) {
      window.addEventListener("scroll", this.handleScroll);
    }
  },
  methods: {
    handleScroll() {
      this.scrollD =
        document.documentElement.scrollTop || document.body.scrollTop;
    },
    btnFun(item) {
      item.btnClick && item.btnClick();
    },
    /// 刷新
    handleRefresh() {
      if (this.$parent.$refs.bTable && this.$parent.dataForm.url) {
        this.$parent.$refs.bTable.reload()
      } else if (this.$parent.$parent.$refs.bTable && this.$parent.$parent.dataForm.url)  {
        this.$parent.$parent.$refs.bTable.reload()
      } else {
        this.$emit('handleRefresh')
      }
    },
    /** 手风琴change事件 */
    onTransition() {
      this.show = !this.show;
    }
  }
};
</script>
  
  <style lang="scss" scoped>
@import "@/styles/variables.scss";
.b-card{
  position: relative;
  background-color: #ffffff;
  margin-bottom: 16px;
  border-radius: 16px;
  padding: 24px;
  // overflow: hidden;
}


.b-card-detail {
  overflow: auto;
  .slot-content {
    position: relative;
    // max-height: calc(100vh - 220px);
    // padding-bottom: 80px;
    overflow: auto;
    padding-right: 10px;
    // 滚动条样式
    &::-webkit-scrollbar {
      width: 6px;
      height: 6px;
    }
    &::-webkit-scrollbar-thumb {
      background: #e4e7ed;
    }
    &::-webkit-scrollbar-track {
      background: #f2f6fc;
    }
  }
  // 底部悬浮按钮
  .btn-wrapper{
    position: absolute;
    display: flex;
    justify-content: center;
    align-items: center;
    left: 0;
    right: 0;
    bottom: 0;
    height: 80px;
    background: #ffffff;
  }
}

.del_card {
  margin: 0;
  padding: 0;
  box-shadow: 0px 0px;
  background-color: #ffffff;
}
.card-head {
  position: relative;
}
.title-content {
  // border-bottom: 1px solid #ebf0f6;
  font-size: 18px;
  font-weight: bold;
}
.row_flex {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.title {
  font-size: 16px;
  color: rgba(0, 0, 0, 0.85);
  font-weight: 400;
}
.subtitle {
  margin-left: 10px;
  font-size: 13px;
}
.btn {
  display: flex;
}
.btn-item {
  min-width: 68px;
  height: 32px;
  border-radius: 60px;
  font-size: 13px;
  line-height: 32px;
  text-align: center;
  margin-right: 5px;
  cursor: pointer;
  user-select: none;
  padding: 0 12px;
}
.btn-item:hover {
  background: rgb(2, 90, 222);
}
.btn-item:last-child {
  margin-right: 0;
}
.default_btn {
  background: #ffffff;
  color: #40465a;
  border: 1px solid #d9d9d9;
}
.primary_btn {
  background: #0f6ffc;
  color: #ffffff;
}
.open_bg {
  width: 20px;
  border-radius: 50%;
  background: #3386fe;
  height: 20px;
  color: #ffffff;
  line-height: 20px;
  text-align: center;
  cursor: pointer;
  position: absolute;
  right: 0;
  top: 0;
}

.refresh-btn {
  position: absolute;
  top: -37px;
  right: 0;
  z-index: 1000;
  color: $theme;
  cursor: pointer;
}
</style>